<?php  
require dirname(__FILE__).'/config.php';
require dirname(__FILE__).'/include/db_connect.php';
require dirname(__FILE__).'/include/session-start.php';
start_session();
if(isset($_SESSION['username'])) {
	header("location:admin/index.php");
	exit();
}
?>

<!DOCTYPE html>
<html>
<head>
		<?php include dirname(__FILE__).'/include/common-header.php' ?>
		<title>登陆</title>
		<script type="text/javascript">
		var _interval;
		$(document).ready(function(){
			$.extend({
			  show:function(){
			   alert("ready");
			  }
			});
			$("#submit-btn").click(
				function() {
					var username = $("#username").val();
					var password = $("#password").val();
					if($.trim(username)==''||$.trim(password)=='') {
						alert("账号或密码不能为空");
						return false;
					} else {
						return true;
					}
				});
			_interval = setInterval("show()",2000);
			<?php
				if(isset($_POST['username'])&&isset($_POST['password'])) {
					echo '$("#username").val("'.$_POST['username'].'");';
					echo '$("#password").val("'.$_POST['password'].'");';
					echo '$("#myModal").modal(show);';
				}
			?>
			});
		function show() {
			$('#myModal').modal(show);
			clearInterval(_interval);
		}
		</script>

		<?php 
		if(isset($_POST['submited'])&&$_POST['submited']==1) {
			$username = $_POST['username'];
			$password = $_POST['password'];
			$username = addslashes($username);
			$password = addslashes($password);
			$verify_code = addslashes(trim($_POST['verify-code']));
			if($verify_code!=$_SESSION['verify_code']) {
				echo '<script type="text/javascript">
							alert("验证码错误!");
						</script>';
			}
			else {
				$password = md5($password);
				$conn = get_db_conn();
				$sql = "select * from user where username='".$username."' and password='".$password
						."' limit 0,1;";
				$ret = mysql_query($sql,$conn);

				if($ret) {
					if(mysql_num_rows($ret)>0) {
						$row = mysql_fetch_array($ret);
						$_SESSION['username']=$username;
						$_SESSION['user_id']=$row['id'];
						$_SESSION['user_role']=$row['user_role'];
						mysql_close($conn);
						echo '<script type="text/javascript">
								window.location="admin/index.php";
							</script>';
						exit();
					} else {
						echo '<script type="text/javascript">
								alert("账号或密码错误!");
							</script>';
					}
				} else {
					echo "error: ".mysql_error();
				}
				mysql_close($conn);
			}
			
		}
		?>


</head>
<body style="background-image:url(http://mu6.bdstatic.com/static/fm/1415086500/themes/public-bg9.jpg)">
	

<div style="margin-top:15%">
	<h1 class="text-center" style="color:#f0f0f0">紫荆书屋后台管理系统</h1>
	  
	  <button class="btn btn-success btn-lg btn-block" 
		style="MARGIN-RIGHT: auto; MARGIN-LEFT: auto;width:50%" 
		data-toggle="modal" data-target="#myModal">
	  		点击此登陆
		</button>
</div>

	<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog">
	    <div class="modal-content">
	      <div class="modal-header">
	        
	        <h4 class="modal-title" id="myModalLabel">登陆</h4>
	      </div>
	      <div class="modal-body">
	        <form class="form-horizontal col-md-offset-3" id="submit-form" role="form" method="post" action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>>
			  <div class="form-group">
			    <label for="username" class="col-sm-2 control-label">用户名</label>
			    <div class="col-sm-10">
			      <input style="width:60%" type="text" class="form-control" id="username" name="username" placeholder="用户名">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="password" class="col-sm-2 control-label">密码</label>
			    <div class="col-sm-10">
			      <input style="width:60%" type="password" class="form-control" id="password" name="password" placeholder="密码">
			    </div>
			  </div>
			  <div class="form-group">
			    <label for="verify-code" class="col-sm-2 control-label">验证码</label>
			    <div class="col-sm-10">
			      <input style="width:60%" type="text" class="form-control" id="verify-code" name="verify-code" placeholder="验证码">
			      <img  title="点击刷新" src="include/verify-code.php" align="absbottom" onclick="this.src='include/verify-code.php?'+Math.random();" style="margin-top:20px;height:40px;"></img>
			    </div>
			  </div>
			  <div class="form-group">
			    <div class="col-sm-offset-2 col-sm-10">
			      <input class="btn btn-primary" id="submit-btn" type="submit" value="登陆">
			    </div>
			  </div>
			  <input name="submited" type="hidden" value="1">
			</form>
	      </div>
	    </div>
	  </div>
	</div>

</body>
</html>